﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Cobit5.Web.Cobit
{
    public class FrameworkItem
    {
        #region Model

        private string _processID;
        /// <summary>
        /// Process ID
        /// </summary>
        public string ProcessID
        {
            get { return _processID; }
            //set { _processID = value; }
        }

        private string _processName;
        /// <summary>
        /// Process Name
        /// </summary>
        public string ProcessName
        {
            get { return _processName; }
            //set { _processName = value; }
        }

        private string _processNameEng;
        /// <summary>
        /// Process Name English
        /// </summary>
        public string ProcessNameEng
        {
            get { return _processNameEng; }
            //set { _processNameEng = value; }
        }

        private int _pRed;
        /// <summary>
        /// Red Number
        /// </summary>
        public int PRed
        {
            get { return _pRed; }
            //set { _pRed = value; }
        }

        private int _pYello;
        /// <summary>
        /// Yello Number
        /// </summary>
        public int PYello
        {
            get { return _pYello; }
            //set { _pYello = value; }
        }

        private int _pGreen;
        /// <summary>
        /// Green Number
        /// </summary>
        public int PGreen
        {
            get { return _pGreen; }
            set { _pGreen = value; }
        }

        #endregion Model

        public FrameworkItem()
        {
            /*** 如果Application中存在：检测时间、是否更新 ***/
            /*** 如果Application中不存在：创建、取值、更新 ***/
            try
            {
                DateTime lastUpdateTime = DateTime.Parse(HttpContext.Current.Application["FrameworkItemUpdateTime"].ToString());
                DateTime currentTime = DateTime.Now;
                TimeSpan mid_time = currentTime - lastUpdateTime;
                if (mid_time.Minutes > 5)
                {
                    HttpContext.Current.Application["FrameworkItems"] = this.UpdateFrameworkItems();
                    HttpContext.Current.Application["FrameworkItemUpdateTime"] = DateTime.Now;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());

                HttpContext.Current.Application.Add("FrameworkItems", this.UpdateFrameworkItems());
                HttpContext.Current.Application.Add("FrameworkItemUpdateTime", DateTime.Now);
            }
        }

        /// <summary>
        /// 初始化、取值一个Framework Item
        /// </summary>
        private FrameworkItem(string in_processID)
        {
            Cobit5.BIZ.Cobit.Process myProcess = new BIZ.Cobit.Process(in_processID);
            this._processID = myProcess.Id;
            this._processName = myProcess.ProcessNameCHN;
            this._processNameEng = myProcess.ProcessName;
            
            this._pRed= Cobit5.BIZ.Cobit.TimeAlarm.CountTimeAlarms("process",myProcess.Id,0,"red");
            this._pYello=Cobit5.BIZ.Cobit.TimeAlarm.CountTimeAlarms("process",myProcess.Id,0,"yellow");
            this._pGreen=Cobit5.BIZ.Cobit.TimeAlarm.CountTimeAlarms("process",myProcess.Id,0,"green");    
            
        }       

        /// <summary>
        /// 更新Framework的Item
        /// Return： 1.FrameworkItem的List
        /// </summary>
        private List<FrameworkItem> UpdateFrameworkItems()
        {
            List<Cobit5.BIZ.Cobit.Process> allProcess = new List<BIZ.Cobit.Process>();
            allProcess = Cobit5.BIZ.Cobit.Process.GetAllProcess("id", 1, Cobit5.BIZ.Cobit.Process.CountAllProcess());

            List<FrameworkItem> allFrameworkItems = new List<FrameworkItem>();
            for (int i = 0; i < allProcess.Count; i++)
            {
                FrameworkItem newFrameworkItem = new FrameworkItem(allProcess[i].Id);
                allFrameworkItems.Add(newFrameworkItem);
            }

            return allFrameworkItems;
        }
    }
}